﻿Imports MySql.Data.MySqlClient

Public Class master_pool
    Inherits DevExpress.XtraEditors.XtraForm
    Private GoogleControl As GoogleControlPOOL
    Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        GoogleControl = New GoogleControlPOOL
        GoogleControl.Dock = DockStyle.Fill
        'Me.Controls.Add(GoogleControl1)
        Me.Panel1.Controls.Add(GoogleControl)
    End Sub
    Sub LoadNow()
        'TODO: This line of code loads data into the 'DS_t03_master_titik.master_titik' table. You can move, or remove it, as needed.
        Me.Master_titikTableAdapter.FillPool(Me.DS_t03_master_titik.master_titik)

    End Sub
    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

    End Sub
    Function Form_Valid()
        Dim Pesan, Validasi As String
        Pesan = "" : Validasi = ""

        If TextBox1.Text = "" Then Pesan += "- Nama TPA Tidak Boleh Kosong " & vbNewLine
        If TextBox2.Text = "" Then Pesan += "- Garis Y Tidak Boleh Kosong " & vbNewLine
        If TextBox3.Text = "" Then Pesan += "- Garis Y Tidak Boleh Kosong " & vbNewLine
        If ComboBox1.Text = "" Then Pesan += "- Status Tidak Boleh Kosong " & vbNewLine
        If Pesan <> "" Then
            Validasi = "Gagal menyimpan data : " & vbNewLine & vbNewLine & Pesan
        End If
        Return Validasi
    End Function
    Sub clearForm()
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        ComboBox1.Text = ""
    End Sub
    Private Sub Btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Save.Click
        Dim myConnString As String
        myConnString = "server=localhost;User Id=root;password=;Persist Security Info=True;database=db_petruk_new"
        Dim ds As New DataSet
        Dim dsconn As New MySqlConnection(myConnString)
        Dim strsql As String = ""

        Try
            If Form_Valid() = "" Then
                If ID_POOL.Text = "" Or ID_POOL.Text = "-1" Then
                    strsql = "INSERT INTO master_titik (NAMA_TPS,LONGITUDE,LATITUDE,STATUS,JENIS) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & ComboBox1.Text & "','TPA')"
                Else
                    strsql = "UPDATE master_titik SET NAMA_TPS = '" & TextBox1.Text & "', LATITUDE = '" & TextBox2.Text & "',LONGITUDE = '" & TextBox3.Text & "',STATUS = '" & ComboBox1.Text & "' WHERE ID_TITIK = '" & ID_POOL.Text & "'"
                End If
                Dim dsDa As New MySqlCommand(strsql, dsconn)
                dsconn.Open()
                dsDa.ExecuteNonQuery()
                'Me.Validate()
                MsgBox("Sukses !", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Saving Data")
                LoadNow()
                clearForm()
            Else
                MsgBox(Form_Valid, MsgBoxStyle.Critical, "Error Data Saving")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub master_pool_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DevExpress.Skins.SkinManager.EnableFormSkins()
        DevExpress.UserSkins.BonusSkins.Register()
        DevExpress.UserSkins.OfficeSkins.Register()
        Me.LookAndFeel.SkinName = "Office 2010 Blue"
        DefaultLookAndFeel1.LookAndFeel.SkinName = "Office 2010 Blue"
        LoadNow()
    End Sub

    Private Sub GroupBox3_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox3.Enter

    End Sub

    Private Sub Master_titikDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Master_titikDataGridView.CellClick
        ID_POOL.Text = Master_titikDataGridView.Rows(e.RowIndex).Cells(0).Value.ToString()
        TextBox1.Text = Master_titikDataGridView.Rows(e.RowIndex).Cells(1).Value.ToString()
        TextBox2.Text = Master_titikDataGridView.Rows(e.RowIndex).Cells(2).Value.ToString()
        TextBox3.Text = Master_titikDataGridView.Rows(e.RowIndex).Cells(3).Value.ToString()
        ComboBox1.Text = Master_titikDataGridView.Rows(e.RowIndex).Cells(5).Value.ToString()
    End Sub

    Private Sub Master_titikDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Master_titikDataGridView.CellContentClick

    End Sub
End Class
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class GoogleControlPOOL : Inherits UserControl

    Private WebBrowser1 As WebBrowser

    Private Panel As Panel

    Private StatusStrip1 As StatusStrip

    Private StatusButtonDelete As ToolStripButton

    Private StatusLabelLatLng As ToolStripStatusLabel

    Private InitialZoom As Integer

    Private InitialLatitude As Double

    Private InitialLongitude As Double

    Private InitialMapType As GoogleMapType

    'I use this enum to store the current map

    'type into the application's settings.

    'So when the user closes the map on Satellite

    'mode it will be on Satellite mode the next 

    'time they open it.

    Public Enum GoogleMapType

        None

        RoadMap

        Terrain

        Hybrid

        Satellite

    End Enum

    Sub New()

        MyBase.New()

        WebBrowser1 = New WebBrowser

        StatusStrip1 = New StatusStrip

        StatusButtonDelete = New ToolStripButton

        StatusLabelLatLng = New ToolStripStatusLabel

        WebBrowser1.Dock = DockStyle.Fill

        WebBrowser1.AllowWebBrowserDrop = False

        WebBrowser1.IsWebBrowserContextMenuEnabled = False

        WebBrowser1.WebBrowserShortcutsEnabled = False

        WebBrowser1.ObjectForScripting = Me

        WebBrowser1.ScriptErrorsSuppressed = False

        AddHandler WebBrowser1.DocumentCompleted, AddressOf WebBrowser1_DocumentCompleted

        StatusStrip1.Dock = DockStyle.Bottom

        StatusStrip1.Items.Add(StatusButtonDelete)

        StatusStrip1.Items.Add(StatusLabelLatLng)

        StatusButtonDelete.Text = "Delete Markers"

        AddHandler StatusButtonDelete.Click, AddressOf StatusButtonDelete_Click

        Me.Controls.Add(WebBrowser1)

        Me.Controls.Add(StatusStrip1)

        'The default map settings.

        InitialZoom = 9

        InitialLatitude = -7.553101

        InitialLongitude = 112.282805

        InitialMapType = GoogleMapType.RoadMap

    End Sub

    Sub New(ByVal zoom As Integer, ByVal lat As Double, ByVal lng As Double, ByVal mapType As GoogleMapType)

        'This constructor could be used to start the map with different values

        'other than the default settings.

        Me.New()

        InitialZoom = zoom

        InitialLatitude = lat

        InitialLongitude = lng

        InitialMapType = mapType

    End Sub

    Private Sub GoogleControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Load the htm doc into the webrowser.

        'When it completes, intialize the map.

        WebBrowser1.DocumentText = My.Computer.FileSystem.ReadAllText("GoogleMap.htm")

    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

        'Initialize the google map with the initial settings.

        'The Initialize script function takes four parameters.

        'zoom, lat, lng, maptype. Call the script passing the

        'parameters in.

        WebBrowser1.Document.InvokeScript("Initialize", New Object() {InitialZoom, InitialLatitude, InitialLongitude, CInt(InitialMapType)})

    End Sub

    Public Sub Map_MouseMove(ByVal lat As Double, ByVal lng As Double)

        'Called from the GoogleMap.htm script when ever the mouse is moved.

        StatusLabelLatLng.Text = "lat/lng: " & CStr(Math.Round(lat, 4)) & " , " & CStr(Math.Round(lng, 4))

        'Form1.txtLong.Text = CStr(Math.Round(lat, 4))

        'Form1.txtLat.Text = CStr(Math.Round(lng, 4))



    End Sub

    Public Sub Map_Click(ByVal lat As Double, ByVal lng As Double)

        'Add a marker to the map.

        'Dim MarkerName As String = MsgBox("Success !!")

        'If Not String.IsNullOrEmpty(MarkerName) Then

        'The script function AddMarker takes three parameters

        'name,lat,lng. Call the script passing the parameters in.

        master_tpa.TextBox2.Text = CStr(Math.Round(lat, 4))

        master_tpa.TextBox3.Text = CStr(Math.Round(lng, 4))
        'MsgBox(CStr(Math.Round(lng, 4)) & CStr(Math.Round(lat, 4)))

        'WebBrowser1.Document.InvokeScript("AddMarker", New Object() {MarkerName, lat, lng})

        'End If

    End Sub

    Public Sub Map_Idle()

        'Would be a good place to load your own custom markers

        'from data source

    End Sub

    Public Sub test(ByVal start As String, ByVal akhir As String, ByVal wp As String)

        'MsgBox("TreeViewHitTestInfo")

        WebBrowser1.Document.InvokeScript("calcRoute", New Object() {start, akhir, wp})

        'MsgBox(akhir)

    End Sub

    Public Sub test2(ByVal start As String, ByVal akhir As String, ByVal wp As String)

        MsgBox(akhir & start)

    End Sub

    Private Sub StatusButtonDelete_Click(ByVal sender As Object, ByVal e As EventArgs)

        'Call the DeleteMarkers script in the htm doc.

        WebBrowser1.Document.InvokeScript("DeleteMarkers")

    End Sub

End Class